package servlet;

import pojo.Book;
import pojo.Category;
import pojo.Publisher;
import service.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Tiler on 2020/11/6
 */
public class GotoSetBook extends HttpServlet {
    private BookService bookService = new BookServiceImpl();

    private CategoryService categoryService = new CategoryServiceImpl();

    private PublisherService publisherService = new PublisherServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");

        String id = req.getParameter("id");

        Book book = bookService.getBookById(Integer.parseInt(id));
        List<Category> categories = categoryService.getAllCategories();
        List<Publisher> publishers = publisherService.getAllPublishers();
        //查询图书分类
        List<Category> list = bookService.getCategoriesByBid(Integer.parseInt(id));
        book.setCategories(list);

        req.setAttribute("book", book);
        req.setAttribute("categories", categories);
        req.setAttribute("publishers", publishers);

        req.getRequestDispatcher("set_book.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
